package com.sigmazero13.bgjeeves.utils;

import java.util.List;

import android.widget.AutoCompleteTextView.Validator;

import com.sigmazero13.bgjeeves.model.GameInfo;

public class GameNameValidator implements Validator {
	
	private List<GameInfo> games;
	
	public GameNameValidator(List<GameInfo> games) {
		this.games = games;
	}

	@Override
	public CharSequence fixText(CharSequence invalidText) {
		if (invalidText == null) {
			return games.get(0).getName();
		}
		String invalidName = invalidText.toString().toLowerCase();
		
		for (GameInfo game : games) {
			if (game.getName().toLowerCase().compareTo(invalidName) >= 0) {
				return game.getName();
			}
		}
		
		return null;
	}

	@Override
	public boolean isValid(CharSequence text) {
		for (GameInfo game : games) {
			if (game.getName().equals(text)) {
				return true;
			}
		}
		
		return false;
	}

}
